package com.myworkspace.standard.common.utils;

import cn.hutool.core.io.resource.ResourceUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * redis 工具类
 */
public class RedisClientUtil {
    private final static Logger LOG = LoggerFactory.getLogger(RedisClientUtil.class);


    private static StringRedisTemplate stringRedisTemplate;

    private static JedisPool jedisPool;

    // redis 地址
    private static final String REDIS_HOST = "r-2ze706dfc4a09a04pd.redis.rds.aliyuncs.com";
    // redis端口
    private static final int REDIS_PORT = 6379;
    // redis获取连接超时 2000毫秒
    private static final int REDIS_TIME_OUT = 2000;
    private static final int DATA_BASE = 0;
    // redis密码
    private static final String REDIS_PASSWORD = "Change123";
    private static final String REDIS_CONFIG_MAX_TOTAL = "8";
    private static final String REDIS_CONFIG_MAX_IDLE = "8";
    private static final String REDIS_CONFIG_MIN_IDLE = "0";
    private static final int REDIS_CONFIG_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 6000;

    private boolean isJedis = true;

    static {
        if (SpringContextUtil.getApplicationContext() != null) {
            stringRedisTemplate = SpringContextUtil.getApplicationContext().getBean(StringRedisTemplate.class);
        }
    }

    /**
     * 初始化连接池
     */
    public static void init() {
        if (jedisPool == null) {
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setTimeBetweenEvictionRunsMillis(60000);
            jedisPool = new JedisPool(poolConfig, REDIS_HOST, REDIS_PORT, REDIS_TIME_OUT, REDIS_PASSWORD, DATA_BASE);
            LOG.info("redis连接池初始化成功==============================");
        }
    }

    /**
     * 销毁连接池
     */
    public static void destroy() {
        if (jedisPool != null) {
            jedisPool.destroy();
            LOG.info("redis连接池销毁成功==============================");
        }
    }

    /**
     * 获取连接
     *
     * @return
     */
    public static Jedis getClient() {
        init();
        return jedisPool.getResource();
    }

    public static void CloseRedis(Jedis redis) {
        redis.close();
    }

    // redis连接池

    public static int setNx(String key, String value, String time) {

        return 0;
    }

    public static String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public static String execute(String scriptStr, List<String> keys, Object... objs) {

        RedisScript<String> script = new DefaultRedisScript<>(scriptStr);
        return RedisClientUtil.stringRedisTemplate.execute(script, keys, objs);
    }

//    private


}
